package 字节;


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main_0918_3 {
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        String line=cin.nextLine();
        int n=line.length();
        if (n%4!=0) {
            System.out.println(-1);
        }
        int everyNum=n/4;
        //统计每个字符出现的次数
        Map<Character,Integer> everyCount=new HashMap<>();
        for (char c:line.toCharArray()) {
            everyCount.put(c,everyCount.getOrDefault(c,0)+1);
        }
        //计算每个字符多出来了几个
        HashMap<Character,Integer> need=new HashMap<>();
        for (Map.Entry<Character,Integer> entry:everyCount.entrySet()) {
            char key=entry.getKey();
            int value=entry.getValue();
            if (value>everyNum){
                need.put(key,value-everyNum);
            }
        }
        //通过滑动窗口，找到最短的覆盖需要的字符的子串长度
        long res=Integer.MAX_VALUE;
        Map<Character,Integer> window=new HashMap<>();
        int left=0,right=0,valid=0;
        while (right<n){
            char c=line.charAt(right);
            right++;
            if (need.containsKey(c)){
                window.put(c,window.getOrDefault(c,0)+1);
                if (window.get(c).equals(need.get(c))){
                    valid++;
                }
            }
            while (valid==need.size()){
                res=Math.min(res,right-left);
                char d=line.charAt(left);
                left++;
                if (need.containsKey(d)){
                    if (window.get(d).equals(need.get(d))){
                        valid--;
                    }
                    window.put(d,window.getOrDefault(d,0)-1);
                }
            }
        }
        System.out.println(res);
    }
}

